﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BuildOptimizer
{
    class BoCommandBuildHive : BoCommand
    {
        private const float _min = 200.0f;
        private const float _gas = 150.0f;
        private const int _time = 100;
        private const commandID _commandID = commandID.buildHive;

        private int _hatchIndex = -1;
        public BoCommandBuildHive(int hatchIndex)
        {
            _hatchIndex = hatchIndex;
        }

        public override void execute(BoGoal goal, BoDecisionNode node)
        {
            node.gameState.removeMineralsAndGas(_min, _gas);
            node.gameState.removeBuilding((int)Buildings.Lair);
            node.gameState.startBuildingBuilding((int)Buildings.Hive);
            node.gameState.getHatchery(_hatchIndex).evolveToHive();

            checkIfMoreRequired(goal, node);
            BoTask t = new BoTask(_time, delegate(BoDecisionNode n)
            {
                n.gameState.finishedBuildingBuilding((int)Buildings.Hive);
                n.gameState.getHatchery(_hatchIndex).makeHive();
                n.unlockCommand((int)commandID.buildUltraliskCavern);
                n.unlockCommand((int)commandID.buildGreaterSpire);
                n.unlockCommand((int)commandID.upgradeAdrenalGlands);
            });
            node.gameState.addTask(t);
        }

        public static bool checkIfMoreRequired(BoGoal goal, BoDecisionNode node)
        {
            int nbRequiredHives = goal.nbBuildingsRequired((int)Buildings.Hive);
            int nbCurrentHives = node.gameState.nbBuildingsBeingBuilt((int)Buildings.Hive) + node.gameState.nbBuildingsBuilt((int)Buildings.Hive);

            if (nbRequiredHives == -1)
            {
                if (nbCurrentHives < 1)
                {
                    return true;
                }
            }
            if (nbCurrentHives < nbRequiredHives)
            {
                return true;
            }

            node.dismissCommand((int)commandID.buildHive);
            return false;
        }
        public static bool isAvailable(BoGameState gs, int hatchIndex)
        {
            if (!gs.getHatchery(hatchIndex).isLair)
            {
                return false;
            }
            if (gs.getHatchery(hatchIndex).isEvolving)
            {
                return false;
            }
            if (gs.getHatchery(hatchIndex).isBuildingQueen)
            {
                return false;
            }
            return true;
        }
        public static bool isAffordable(BoGameState gs)
        {
            if (!gs.isResourcesAvailable(_min, _gas, 0))
            {
                return false;
            }
            return true;
        }

        public override int getCommandCode()
        {
            int code = ((int)_commandID * 100) + _hatchIndex;
            return code;
        }
        public override commandID getCommandID()
        {
            return _commandID;
        }
        public override string toString()
        {
            string str = "Lair " + _hatchIndex + " -> Hive";
            return str;
        }
    }
}
